// 封装和历史记录相关的接口函数
import request from '@/util/http'

// 统一联系人查询接口（合并分页/分组/搜索）
export const fetchContacts = ({ 
    pageNo = 1, 
    pageSize = 10, 
    groupId, 
    keyword,
    phoneNumber,
    userId,
  }) => {
    return request({
      url: '/contacts',
      method: 'GET',
      params: {
        pageNo,
        pageSize,
        groupId: groupId || undefined, // 空值不传参数
        keyword: keyword || undefined,
        phoneNumber,
        userId
      }
    })
  }

//验证注册
export const checkRegister = (params) => {
    return request({
        url: "/user/register",
        method: 'POST',
        data: params     
    })
}

//验证登录
export const checkLogin = (params) => {
    return request({
        url: "/user/login",
        method: 'POST',
        data: params
    })
}

// 新增联系人信息
export const addFriendInfo = (params) => {
    return request({
        url: "/contacts/add",
        method: 'POST',
        data: params
    })
}

// 更新联系人信息
export const updateFriendInfo = (params) => {
    return request({
        url: "/contacts/update", 
        method: 'POST',
        data: params
    })
}

// 删除联系人信息
export const deleteFriendInfo = (params) => {
    return request({
        url: "/contacts/delete",
        method: 'POST',
        data: params
    })
}

// 获取分组
export const getGroup = (phoneNumber) => {
    return request({
        url: "/group/get",
        method: 'GET',
        params: {
          phoneNumber
         }
    }) 
}

// 添加分组
// export const addGroup = (params) => {
//     return request({
//         url:"/group/add",
//         method:'POST',
//         data:params
//     })
// }

// 封装请求函数（确保字段名正确，使用 POST 请求体传递参数）
export const addGroup = (params) => {
  return request({
      url: "/group/add",
      method: "POST",
      data: params // 直接传递包含 groupName 和 userId 的对象
  });
}

// 删除分组
export const deleteGroup = (groupId) => {
  return request({
      url: "/group/delete",
      method: 'POST',
      headers: {
          'Content-Type': 'application/json'
      },
      data: groupId
  });
}
  // 批量添加联系人到分组
  export const batchAddGroups = ({ contactId, newGroupId }) => {
    return request({
        url: '/contacts/batch-groups',
        method: 'POST',
        data: { contactId, newGroupId }
    });
}

// 前端 api 封装修正
// export const importContacts = (formData) => {
//   return request({
//     url: '/contacts/import',
//     method: 'POST',
//     data: formData, 
//     timeout: 60000
//   })
// }

// cat：2025/5/16
export const importContacts = (formData) => {
  return request({
    url: '/contacts/import',
    method: 'POST',
    data: formData,
    timeout: 60000,
    headers: {
      // 注意：这里不要手动设置 Content-Type，让浏览器自动生成带 boundary 的 header
      // 'Content-Type': 'multipart/form-data' // 注释掉这行！
    },
    processData: false, // 禁止 axios 处理数据
    contentType: false // 禁止 axios 设置 Content-Type
  })
}